<?php require_once('Connections/locamedicadmin.php'); ?>

<?php include('userValidation.php'); ?>

<?php require_once('functions.php'); ?>

<?php

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  	$file = strtolower($_FILES["uploadedfile"]["name"]);
	$ext = strtolower(substr($file,-3,3));
	$imagen = date("dmYHis").".".$ext;
	$target_path = "../images/csre/banner/" . $imagen; 
	
	if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
		include('SimpleImage.php');
		$image = new SimpleImage();
		$image->load($target_path);
		$ancho = $image->getWidth();
		$alto = $image->getHeight();
		if ($ancho > 560) {
			$alto = 560 * $alto / $ancho;
			$ancho = 560;
		}
		$image->resize($ancho, $alto);
		$image->save($target_path);
	}

  $insertSQL = sprintf("INSERT INTO csre_banner (path, selected) VALUES (%s, 0)",
                       GetSQLValueString($imagen, "text"));

   mysql_select_db($database_locamedicadmin, $locamedicadmin);
   mysql_query($insertSQL, $locamedicadmin) or die(mysql_error());

   header(sprintf("Location: %s", "banner_editor.php"));

}


$maxRows_banners = 10;
$pageNum_banners = 0;
if (isset($_GET['pageNum_banners'])) {
  $pageNum_banners = $_GET['pageNum_banners'];
}
$startRow_banners = $pageNum_banners * $maxRows_banners;

mysql_select_db($database_locamedicadmin, $locamedicadmin);
$query_banners = "SELECT * FROM csre_banner";
$query_limit_banners = sprintf("%s LIMIT %d, %d", $query_banners, $startRow_banners, $maxRows_banners);
$banners = mysql_query($query_limit_banners, $locamedicadmin) or die(mysql_error());
$row_banners = mysql_fetch_assoc($banners);

if (isset($_GET['totalRows_banners'])) {
  $totalRows_banners = $_GET['totalRows_banners'];
} else {
  $all_banners = mysql_query($query_banners);
  $totalRows_banners = mysql_num_rows($all_banners);
}
$totalPages_banners = ceil($totalRows_banners/$maxRows_banners)-1;

?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Locamedic CSRE Admin</title>
<link href="locamedicadmin.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="ajax_functions.js" type="text/javascript"></script>
</head>
<body>
<div id="page">

	<?php include('sidebar.php'); ?>
	
	<!-- end sidebar -->
	<div id="content">
		<div align="center"><img src="images/page_image_5.jpg" alt="" /></div>
	  <div class="boxed">
			<h1 class="title2">Banni&egrave;re &Eacute;diteur</h1>

			<h2>Ajouter une Banni&egrave;re</h2>

			<form enctype="multipart/form-data" method="post" name="form1" action="<?php echo $editFormAction; ?>" >
                <input type="hidden" name="MM_insert" value="form1" />
				<table>
                	<tr valign="baseline">
                    	<td nowrap>Ajouter une Banni&egrave;re</td>
                    	<td><input type="file" name="uploadedfile" value="" size="33" id="foto"></td>
                  	</tr>
				</table>
				<div class="botonera">
					<input type="submit" value="Enregistrer"  />
				</div>
			</form>

			<h2>Banni&egrave;re List</h2>

			<table cellpadding="5" cellspacing="0">
              <thead>
                <td>S&eacute;lectionnez</td>
                <td align="center">s&eacute;lectionn</td>
				<td></td>
              </thead>
              <?php do { ?>
                <tr>
                  <td><img src="../images/csre/banner/<?php echo $row_banners['path']; ?>" alt="<?php echo $row_banners['path']; ?>" /></td>
                  <td align="center"><input onclick="selectBanner(<?php echo $row_banners['id_banner']; ?>);" type="radio" name="selectedbanner" value="<?php echo $row_banners['id_banner']; ?>" 
						<?php if ($row_banners['selected']) { echo "checked=\"checked\""; } ?> />
				  </td>
				  <td>
				  	<a href="banner_delete.php?id_banner=<?php echo $row_banners['id_banner']; ?>" 
						onclick="return confirm('Etes-vous sur de vouloir supprimer ce registre?');">
						<img src="images/1307905898_edit-trash.png" alt="Supprimer" border="0" />
					</a>
				  </td>
                </tr>
                <?php } while ($row_banners = mysql_fetch_assoc($banners)); ?>
            </table>
			
			<table border="0" width="50%" align="center">
          <tr>
            <td width="23%" align="center"><?php if ($pageNum_banners > 0) { // Show if not first page ?>
                  <a href="<?php printf("%s?pageNum_banners=%d%s", $currentPage, 0, $queryString_banners); ?>"><img src="images/resultset_first.png" border=0></a>
                  <?php } // Show if not first page ?>
            </td>
            <td width="31%" align="center"><?php if ($pageNum_banners > 0) { // Show if not first page ?>
                  <a href="<?php printf("%s?pageNum_banners=%d%s", $currentPage, max(0, $pageNum_banners - 1), $queryString_banners); ?>"><img src="images/resultset_previous.png" border=0></a>
                  <?php } // Show if not first page ?>
            </td>
            <td width="23%" align="center"><?php if ($pageNum_banners < $totalPages_banners) { // Show if not last page ?>
                  <a href="<?php printf("%s?pageNum_banners=%d%s", $currentPage, min($totalPages_banners, $pageNum_banners + 1), $queryString_banners); ?>"><img src="images/resultset_next.png" border=0></a>
                  <?php } // Show if not last page ?>
            </td>
            <td width="23%" align="center"><?php if ($pageNum_banners < $totalPages_banners) { // Show if not last page ?>
                  <a href="<?php printf("%s?pageNum_banners=%d%s", $currentPage, $totalPages_banners, $queryString_banners); ?>"><img src="images/resultset_last.png" border=0></a>
                  <?php } // Show if not last page ?>
            </td>
          </tr>
        </table>
			
	  </div>
  </div>
	<!-- end content -->
	<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->

<?php include('footer.php'); ?>

</body>
</html>
<?php
mysql_free_result($banners);

?>